home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Pyramid / Source / matrix.subproj / perspect.cc < prev    next >
C/C++ Source or Header  |  1993-09-15  |  3KB  |  141 lines

  1. /*    perspect.cc - matrix algebra definitions
  2.  *    Copyright (C) 1993 Corona Design, Inc. All rights reserved.
  3.  *
  4.  *    Abstract
  5.  *        Common 3D perspective transformations. Provides an arbitrary
  6.  *        view plane with a center of perspective 1 unit (in VIEW space)
  7.  *        from the view plane.
  8.  *
  9.  *        Reference: Foley & van Dam, Computer Graphics
  10.  *        C callable.
  11.  *
  12.  *    RCS path: 
  13.  *        $Source: /Users/pkron/Projects/voxel/Pyramid/matrix.subproj/RCS/perspect.cc,v $
  14.  *    Modified: $Date: 93/09/15 12:34:01 $ by $Author: pkron $
  15.  *    Current State: $State: Exp $ locked by $Locker:  $
  16.  */
  17.  
  18. #include    "standard.h"
  19. #include    "matrix.h"
  20. #include    "perspect.h"
  21.  
  22.  
  23.                                 // create an identity matrix
  24. MATRIX
  25. allocIdentity()
  26.     {
  27.     MATRIX    matrix = new struct _matrix;
  28.     
  29.     *matrix = unitMatrix;
  30.     return( matrix);
  31.     }
  32.  
  33.  
  34. void
  35. freeMatrix( MATRIX matrix)
  36.     {
  37.     delete matrix;
  38.     }
  39.  
  40.  
  41.                                 // multiply matrix by point
  42. void
  43. map( MATRIX matrix, POINT *aPoint)
  44.     {
  45.     ((VECTOR *)aPoint)->matrixMultiply( matrix);
  46.     }
  47.  
  48.  
  49.                                 // define the matrix such that
  50.                                 // the viewing transform is normal
  51.                                 // to the given vector "normal"
  52.                                 // and the vector "up" projects onto
  53.                                 // the positive Y-axis
  54. void    
  55. orient( MATRIX matrix, POINT *normal, POINT *up)
  56.     {
  57.     VECTOR    rX = unitVector;
  58.     VECTOR    rY = unitVector;
  59.     VECTOR    rZ = unitVector;
  60.     
  61.                                 // compute axis unit vectors
  62.     rZ = *(VECTOR *)normal;
  63.     rZ.scalarMultiply( 1/rZ.vectorLength());
  64.     
  65.     rX = *(VECTOR *)up;
  66.     rX.crossProduct( &rZ);
  67.     rX.scalarMultiply( 1/rX.vectorLength());
  68.     
  69.     rY = rZ;
  70.     rY.crossProduct( &rX);
  71.     
  72.                                 // now build rotation matrix
  73.     *matrix = unitMatrix;
  74.     *matrix->getRx() = rX;
  75.     *matrix->getRy() = rY;
  76.     *matrix->getRz() = rZ;
  77.     }    
  78.         
  79.  
  80.                                 // project a point onto the viewplane
  81. void
  82. project( POINT *aPoint)
  83.     {
  84.     ((VECTOR *)aPoint)->scalarMultiply( 1/(*aPoint)[2]);
  85.     }
  86.  
  87.  
  88. void
  89. scaleProjection( MATRIX transform, int width, int height)
  90.     {
  91.                             // projection matrix
  92.     struct _matrix    scale = unitMatrix;    
  93.     
  94.                             // scale and translate origin
  95.                             // to lower left
  96.                             // (no translation needed for z)    
  97.     scale.matrix[0][0] = width;
  98.     scale.matrix[1][1] = height;
  99.     transform->matrixMultiply( &scale);
  100.     }
  101.     
  102.     
  103. void    
  104. translate( MATRIX matrix, POINT *to)
  105.     {
  106.     matrix->matrix[0][3] -= (*to)[0];
  107.     matrix->matrix[1][3] -= (*to)[1];
  108.     matrix->matrix[2][3] -= (*to)[2];
  109.     }
  110.     
  111.     
  112. void
  113. viewDistance( MATRIX matrix, float distance)
  114.     {
  115.     POINT    COP;
  116.     struct _matrix    Mper;    // foreshortening transform
  117.     
  118.     COP[0] = 0;
  119.     COP[1] = 0;
  120.     COP[2] = distance;        // eye is this distance out
  121.     COP[3] = 1;
  122.     translate( matrix, &COP);
  123.     
  124.                             // account for distance foreshortening
  125.     Mper = unitMatrix;
  126.     Mper.matrix[2][2] = -1; // put eye on negative z axis
  127.     matrix->matrixMultiply( &Mper);
  128.     }
  129.  
  130.  
  131. #ifdef    _LOG
  132. /*
  133.  *    $Log:    perspect.cc,v $
  134. Revision 1.1  93/09/15  12:34:01  pkron
  135. Created
  136.  
  137.  */
  138. #endif        
  139.     
  140.     
  141.